<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="我们上一节的这个液晶滚屏移动程序，大概有 160 行左右。随着我们硬件模块使用的增多，程序量的增大，我们往往要把程序写到多个文件里，方便代码的编写、维护和移植。 比如这个液晶滚屏程序，我们就可以把 1602 底层的功能函数专门写到一个.c 文件内，如LcdWaitReady、LcdWriteCmd、LcdWriteDat、LcdShowStr、LcdSetCursor、InitLcd1602 这些">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="13.3、多个.c文件的初步认识">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu087/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="我们上一节的这个液晶滚屏移动程序，大概有 160 行左右。随着我们硬件模块使用的增多，程序量的增大，我们往往要把程序写到多个文件里，方便代码的编写、维护和移植。 比如这个液晶滚屏程序，我们就可以把 1602 底层的功能函数专门写到一个.c 文件内，如LcdWaitReady、LcdWriteCmd、LcdWriteDat、LcdShowStr、LcdSetCursor、InitLcd1602 这些">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-01-28T12:55:48.317Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="13.3、多个.c文件的初步认识">
<meta name="twitter:description" content="我们上一节的这个液晶滚屏移动程序，大概有 160 行左右。随着我们硬件模块使用的增多，程序量的增大，我们往往要把程序写到多个文件里，方便代码的编写、维护和移植。 比如这个液晶滚屏程序，我们就可以把 1602 底层的功能函数专门写到一个.c 文件内，如LcdWaitReady、LcdWriteCmd、LcdWriteDat、LcdShowStr、LcdSetCursor、InitLcd1602 这些">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu087/">





  <title>13.3、多个.c文件的初步认识 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu087/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">13.3、多个.c文件的初步认识</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:37:10+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>我们上一节的这个液晶滚屏移动程序，大概有 160 行左右。随着我们硬件模块使用的增多，程序量的增大，我们往往要把程序写到多个文件里，方便代码的编写、维护和移植。</p>
<p>比如这个液晶滚屏程序，我们就可以把 1602 底层的功能函数专门写到一个.c 文件内，如LcdWaitReady、LcdWriteCmd、LcdWriteDat、LcdShowStr、LcdSetCursor、InitLcd1602 这些函数，都是属于液晶底层驱动的程序代码，我们要使用液晶功能的时候，只有两个函数对我们实际功能实现部分有用，一个是 InitLcd1602 这个函数，因为需要先初始化液晶，另外一个就是 LcdShowStr 这个函数，我们只需要把要显示的内容通过参数传递给这个函数，这个函数就可以实现我们想要的显示效果，所以我们把这几个底层的液晶驱动程序都放到另外一个文件 Lcd1602.c 文件中，而我们想实现的一些比如滚动实现、中断等上层功能程序全部都放到 main.c 中，但是 main.c 文件如何调用 Lcd1602.c 文件中的函数呢？</p>
<p>C 语言中，有一个 extern 关键字，它有两个基本作用。</p>
<p>1、当一个变量的声明不在文件的开头，在它声明之前的函数想要引用的话，则应该用extern 进行“外部变量”声明。用一个简单的程序给大家介绍一下，知道这么回事，能看懂别人写的就行，自己写就别这么用了。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit LED = P0^0;</span><br><span class="line">void main()&#123;</span><br><span class="line">    extern unsigned int i;</span><br><span class="line">    while(1)&#123;</span><br><span class="line">        LED = 0; //点亮小灯</span><br><span class="line">        for(i=0;i&lt;30000;i++); //延时</span><br><span class="line">        LED = 1; //熄灭小灯</span><br><span class="line">        for(i=0;i&lt;30000;i++); //延时</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">unsigned int i = 0;</span><br><span class="line">// ... ...</span><br></pre></td></tr></table></figure>
<p>变量的作用域，是从声明这个变量开始往后所有的程序，如果我们调用在前，声明在后，那么就是这么用。但是实际开发过程中，我们一般都不会这样做，所以仅仅是表达一下 extern的这个用法，但它并不实用。</p>
<p>2、在一个工程中，我们为了方便管理和维护代码，用了多个.c 源文件，如果其中一个main.c 文件要调用 Lcd1602.c 文件里的变量或者函数的时候，我们就必须得在 main.c 里边进行一下外部声明，告诉编译器这个变量或者函数是在其它文件中定义的，可以直接在这个文件中进行调用。</p>
<p>多.c 文件的编程方式，大家不要想象的太复杂。首先新建一个工程，一个工程代表一个完整的单片机程序，只能生成一个 hex，但是一个工程可以有很多个.c 源文件组成共同参与编译。工程建立好之后，新建文件并且保存取名为 main.c 文件，再新建一个文件并且保存取名为 Lcd1602.c 文件，下面我们就可以在两个不同文件中分别编写代码了。当然，在编写程序的过程中，不是说我们要先把 main.c 的文件全部写完，再进行 1602.c 程序的编写，而往往是交互的。比如我们先写 Lcd1602.c 文件中部分 Lcd1602 液晶的底层函数 LcdWaitReady、LcdWriteCmd、LcdWriteDat、InitLcd1602，然后编写 main.c 文件中的功能程序，在编写 main.c文件中程序时，又有对 Lcd1602.c 底层程序的综合调用，这个时候需要 Lcd1602.c 文件提供一个被调用的函数比如 LcdShowStr，我们就可以再到 Lcd1602.c 中把这个函数完成。当然了，这仅仅是一个说明例子而已，顺序完全是没有一个标准的，实际应用中如果对程序逻辑需求了解透彻，根据自己的理解去写程序即可。那我们把 1602 整屏移动的程序改造成为多文件的程序，大家先初步认识一下。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">/***************************Lcd1602.c 文件程序源代码*****************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">#define LCD1602_DB P0</span><br><span class="line">sbit LCD1602_RS = P1^0;</span><br><span class="line">sbit LCD1602_RW = P1^1;</span><br><span class="line">sbit LCD1602_E = P1^5;</span><br><span class="line">/* 等待液晶准备好 */</span><br><span class="line">void LcdWaitReady()&#123;</span><br><span class="line">    unsigned char sta;</span><br><span class="line">    LCD1602_DB = 0xFF;</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 1;</span><br><span class="line">    do &#123;</span><br><span class="line">        LCD1602_E = 1;</span><br><span class="line">        sta = LCD1602_DB; //读取状态字</span><br><span class="line">        LCD1602_E = 0;</span><br><span class="line">    //bit7 等于 1 表示液晶正忙，重复检测直到其等于 0 为止</span><br><span class="line">    &#125;while (sta &amp; 0x80);</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节命令，cmd-待写入命令值 */</span><br><span class="line">void LcdWriteCmd(unsigned char cmd)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 0;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = cmd;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 向 LCD1602 液晶写入一字节数据，dat-待写入数据值 */</span><br><span class="line">void LcdWriteDat(unsigned char dat)&#123;</span><br><span class="line">    LcdWaitReady();</span><br><span class="line">    LCD1602_RS = 1;</span><br><span class="line">    LCD1602_RW = 0;</span><br><span class="line">    LCD1602_DB = dat;</span><br><span class="line">    LCD1602_E = 1;</span><br><span class="line">    LCD1602_E = 0;</span><br><span class="line">&#125;</span><br><span class="line">/* 设置显示 RAM 起始地址，亦即光标位置，(x,y)-对应屏幕上的字符坐标 */</span><br><span class="line">void LcdSetCursor(unsigned char x, unsigned char y)&#123;</span><br><span class="line">    unsigned char addr;</span><br><span class="line">    if (y == 0)&#123; //由输入的屏幕坐标计算显示 RAM 的地址</span><br><span class="line">        addr = 0x00 + x; //第一行字符地址从 0x00 起始</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        addr = 0x40 + x; //第二行字符地址从 0x40 起始</span><br><span class="line">    &#125;</span><br><span class="line">    LcdWriteCmd(addr | 0x80); //设置 RAM 地址</span><br><span class="line">&#125;</span><br><span class="line">/* 在液晶上显示字符串，(x,y)-对应屏幕上的起始坐标，</span><br><span class="line">    str-字符串指针，len-需显示的字符长度 */</span><br><span class="line">void LcdShowStr(unsigned char x, unsigned char y,</span><br><span class="line">        unsigned char *str, unsigned char len)&#123;</span><br><span class="line">    LcdSetCursor(x, y); //设置起始地址</span><br><span class="line">    while (len--)&#123; //连续写入 len 个字符数据</span><br><span class="line">        LcdWriteDat(*str++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 初始化 1602 液晶 */</span><br><span class="line">void InitLcd1602()&#123;</span><br><span class="line">    LcdWriteCmd(0x38); //16*2 显示，5*7 点阵，8 位数据接口</span><br><span class="line">    LcdWriteCmd(0x0C); //显示器开，光标关闭</span><br><span class="line">    LcdWriteCmd(0x06); //文字不动，地址自动+1</span><br><span class="line">    LcdWriteCmd(0x01); //清屏</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line">/*****************************main.c 文件程序源代码******************************/</span><br><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">bit flag500ms = 0; //500ms 定时标志</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">//待显示的第一行字符串</span><br><span class="line">unsigned char code str1[] = &quot;Kingst Studio&quot;;</span><br><span class="line">//待显示的第二行字符串，需保持与第一行字符串等长，较短的行可用空格补齐</span><br><span class="line">unsigned char code str2[] = &quot;Let&apos;s move...&quot;;</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">extern void InitLcd1602();</span><br><span class="line">extern void LcdShowStr(unsigned char x, unsigned char y,</span><br><span class="line">unsigned char *str, unsigned char len);</span><br><span class="line">void main()&#123;</span><br><span class="line">    unsigned char i;</span><br><span class="line">    unsigned char index = 0; //移动索引</span><br><span class="line">    unsigned char pdata bufMove1[16+sizeof(str1)+16]; //移动显示缓冲区 1</span><br><span class="line">    unsigned char pdata bufMove2[16+sizeof(str2)+16]; //移动显示缓冲区 2</span><br><span class="line">    EA = 1; //开总中断</span><br><span class="line">    ConfigTimer0(10); //配置 T0 定时 10ms</span><br><span class="line">    InitLcd1602(); //初始化液晶</span><br><span class="line">    //缓冲区开头一段填充为空格</span><br><span class="line">    for (i=0; i&lt;16; i++)&#123;</span><br><span class="line">        bufMove1[i] = &apos; &apos;;</span><br><span class="line">        bufMove2[i] = &apos; &apos;;</span><br><span class="line">    &#125;</span><br><span class="line">    //待显示字符串拷贝到缓冲区中间位置</span><br><span class="line">    for (i=0; i&lt;(sizeof(str1)-1); i++)&#123;</span><br><span class="line">        bufMove1[16+i] = str1[i];</span><br><span class="line">        bufMove2[16+i] = str2[i];</span><br><span class="line">    &#125;</span><br><span class="line">    //缓冲区结尾一段也填充为空格</span><br><span class="line">    for (i=(16+sizeof(str1)-1); i&lt;sizeof(bufMove1); i++)&#123;</span><br><span class="line">        bufMove1[i] = &apos; &apos;;</span><br><span class="line">        bufMove2[i] = &apos; &apos;;</span><br><span class="line">    &#125;</span><br><span class="line">    while (1)&#123;</span><br><span class="line">        if (flag500ms)&#123; //每 500ms 移动一次屏幕</span><br><span class="line">            flag500ms = 0;</span><br><span class="line">            //从缓冲区抽出需显示的一段字符显示到液晶上</span><br><span class="line">            LcdShowStr(0, 0, bufMove1+index, 16);</span><br><span class="line">            LcdShowStr(0, 1, bufMove2+index, 16);</span><br><span class="line">            //移动索引递增，实现左移</span><br><span class="line">            index++;</span><br><span class="line">            if (index &gt;= (16+sizeof(str1)-1))&#123; //起始位置达到字符串尾部后即返回从头开始</span><br><span class="line">                index = 0;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 12; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">   </span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，定时 500ms */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    static unsigned char tmr500ms = 0;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    tmr500ms++;</span><br><span class="line">    if (tmr500ms &gt;= 50)&#123;</span><br><span class="line">        tmr500ms = 0;</span><br><span class="line">        flag500ms = 1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们在 main.c 中要调用 Lcd1602.c 文件中的 InitLcd1602()和 LcdShowStr 这两个函数，只需要在 main.c 中进行 extern 声明即可。大家用 Keil 软件编程试试，真正的感觉一下多.c 源文件的好处。如果这个程序给你的感觉还不深刻，那下面我们来做一个稍微大点的程序来体会一下。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu086/" rel="next" title="13.2、1602整屏移动">
                <i class="fa fa-chevron-left"></i> 13.2、1602整屏移动
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu088/" rel="prev" title="13.4、单片机计算器实例">
                13.4、单片机计算器实例 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
